package com.permissionmanager.controller;

import com.permissionmanager.common.Result;
import com.permissionmanager.entity.Page;
import com.permissionmanager.entity.Perm;
import com.permissionmanager.service.impl.PermServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * perm控制层
 *
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/perm")
public class PermController {
    private final PermServiceImpl permService;

    // 分页查询权限（pageNum、pageSize、name）
    @GetMapping("/findByPage")
    public Result findByPage(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                             @RequestParam String name) {
        log.info("进入perm分页查询功能！");
        Page<Perm> page = permService.findPage(pageNum,pageSize,name);
        log.info("查询成功！");
        return Result.success(page);
    }

    // 查询父级权限菜单
    @GetMapping("/isFMenu")
    public Result findOne() {
        log.info("进入父级权限菜单查询");
        List<Perm> perm = permService.findPerm(null, null, null, 0);
        log.info("查询成功");
        return Result.success(perm);
    }

    // 新增（id、name、path、isOne）
    @PostMapping("/add")
    public Result add(@RequestBody Perm perm) {
        log.info("进入perm新增功能！");
        log.info("perm-->{}",perm);

        permService.add(perm);
        log.info("新增成功！");
        return Result.success();
    }

    // 修改 （id）
    @PutMapping("/update")
    public Result update(@RequestBody Perm perm) {
        log.info("进入perm修改功能！");
        log.info("perm-->{}",perm);
        permService.update(perm);
        log.info("修改成功！");
        return Result.success();
    }

    // 删除（id）
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable Integer id) {
        log.info("进入perm单条删除功能！");
        permService.del(id);
        log.info("删除成功！");
        return Result.success();
    }


}
